﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Xml.Linq;
using System.Web.Services;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GHRMS.BLL;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Project_Manager : System.Web.UI.Page
    {
        private IProject projectBLL;
        private ComboBLL comboBLL = new ComboBLL();
        private EmployeeBLL empBLL = new EmployeeBLL();
        private string type, jobID;

        protected void Page_Load(object sender, EventArgs e)
        {
            type = Request.QueryString["type"].ToString();
            if (type.Length > 0)
                projectBLL = Project_Factory.GetProjectFactory(type);

            if (!IsPostBack)
            {
                if (!Object.Equals(Session["ProjectID"], null))
                {
                    lbProjectID.Text = Session["ProjectID"].ToString();
                }

                if (!Object.Equals(Session["ProjectName"], null))
                {
                    litTitle.Text = Session["ProjectName"].ToString();
                }
                LoadData();
                LoadEmployee();
            }
        }

        private void LoadData()
        {
            DataTable dt = projectBLL.GetDataProjectManager(lbProjectID.Text);
            Grid_t9.DataSource = dt;
            Grid_t9.DataBind();
        }

        private void LoadEmployee()
        {
            DataTable infoList = empBLL.ListEmployeePosByType("1");
            ddlEmployee.DataTextField = "full_name";
            ddlEmployee.DataValueField = "sequence";
            ddlEmployee.DataSource = infoList;
            ddlEmployee.DataBind();
        }

        private void clear_t9()
        {
            ddlEmployee.ClearSelection();
            ddlEmployee.Text = string.Empty;
        }
        private void bindTab9_forEdit(string seq)
        {
            lblSeq.Text = seq;
            DataRow dr = projectBLL.GetDataProjectManager(seq).Rows[0];
            ddlEmployee.SelectedValue = dr["sequence"].ToString();
        }

        //---- event control
        protected void Grid_t9_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetInsertClick")
            {
                PanelAddEdit_t9.Visible = true;
                clear_t9();
                btnSave_t9.Visible = true;
                btnEdit_t9.Visible = false;
            }
            //else if (e.CommandName == RadGrid.EditCommandName)
            //{
            //    PanelAddEdit_t9.Visible = true;
            //    clear_t9();
            //    btnSave_t9.Visible = false;
            //    btnEdit_t9.Visible = true;
            //    bindTab9_forEdit((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString());

            //}
            else if (e.CommandName == "Delete")
            {
                int nRowSelected = 0;
                foreach (GridDataItem data in Grid_t9.MasterTableView.GetSelectedItems())
                {
                    projectBLL.DeleteProjectManager(data.OwnerTableView.DataKeyValues[data.ItemIndex]["bgm_proj_mng_seq"].ToString());
                    nRowSelected++;
                }
                PanelAddEdit_t9.Visible = false;
            }
            else
            { 
            
            }
            LoadData();
        }

        protected void Grid_t9_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }

        protected void btnCancel_t9_Click(object sender, EventArgs e)
        {
            PanelAddEdit_t9.Visible = false;
            clear_t9();
        }

        protected void btnSave_t9_Click(object sender, EventArgs e)
        {
            try
            {
                string[] strEmp = ddlEmployee.SelectedValue.Split('-');
                projectBLL.InsertProjectManager(lbProjectID.Text, strEmp[0]);
                RadAjaxManager1.Alert("บันทึกเสร็จเรียบร้อย");
            }
            catch
            {
                RadAjaxManager1.Alert("เกิดข้อผิดพลาดในการบันทึก");
            }
            LoadData();
            clear_t9();
            PanelAddEdit_t9.Visible = false;
        }

        protected void btnEdit_t9_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    projectBLL.updateproject
            //    RadAjaxManager1.Alert("บันทึกเสร็จเรียบร้อย");
            //}
            //catch
            //{
            //    RadAjaxManager1.Alert("เกิดข้อผิดพลาดในการบันทึก");
            //}
            //LoadData();
            //clear_t9();
            //PanelAddEdit_t9.Visible = false;
        }

        protected void Grid_t9_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem commandItem = e.Item as GridCommandItem;
                LinkButton button = commandItem.FindControl("AddNew_t9") as LinkButton;
                LinkButton buttonDel = commandItem.FindControl("Delete_t9") as LinkButton;
                if ((bool)Session["Approve"])
                {
                    //button.Enabled = false;
                    //button.Visible = true;
                    //button.Text = "";

                    //buttonDel.Enabled = false;
                    //buttonDel.Visible = true;
                    //buttonDel.Text = "";
                }
            }
        }
    }
}
